package com.ipzoe.common.controller.admin;

import com.ipzoe.common.bean.entity.Admin;
import com.ipzoe.common.bean.entity.Role;
import com.ipzoe.common.bean.response.Code;
import com.ipzoe.common.bean.response.PageBean;
import com.ipzoe.common.bean.response.Response;
import com.ipzoe.common.service.AdminService;
import com.ipzoe.common.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台管理员控制器
 * <p>
 * Created 2018/01/19.
 *
 * @author pfzhang
 */
@Api(description = "后台管理员控制器")
@RestController("adminAdminController")
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    /**
     * 查看管理员列表
     *
     * @param key  搜索关键词
     * @param page 当前页
     * @param size 页面大小
     * @return 管理员集合分页对象
     */
    @ApiOperation("查看管理员列表")
    @PostMapping("/list")
    public Response<PageBean<Admin>> list(@ApiParam("搜索关键词") @RequestParam(required = false, defaultValue = "") String key,
                                          @ApiParam("当前页") @RequestParam(required = false, defaultValue = "1") int page,
                                          @ApiParam("页面大小") @RequestParam(required = false, defaultValue = "10") int size) {
        return adminService.listAdmin(key, page, size);
    }

    /**
     * 创建管理员
     *
     * @param username 用户名
     * @param roleId   角色ID
     * @return 接口响应对象
     */
    @ApiOperation("创建管理员")
    @PostMapping("/create")
    public Response create(@ApiParam("用户名") @RequestParam String username,
                           @ApiParam("角色ID") @RequestParam Long roleId) {
        return adminService.create(username, roleId);
    }

    /**
     * 修改管理员
     *
     * @param adminId  管理员ID
     * @param username 用户名
     * @param roleId   角色ID
     * @return 接口响应对象
     */
    @ApiOperation("修改管理员")
    @PostMapping("/update")
    public Response update(@ApiParam("管理员ID") @RequestParam Long adminId,
                           @ApiParam("用户名") @RequestParam String username,
                           @ApiParam("角色ID") @RequestParam Long roleId) {
        return adminService.update(adminId, username, roleId);
    }

    @ApiOperation("冻结/解冻管理员")
    @PostMapping("/block")
    public Response block(@ApiParam(hidden = true) Admin admin,
                          @ApiParam("管理员ID") @RequestParam Long adminId) {
        if (admin.getId().equals(adminId) || adminId.equals(1L)) {
            return Response.ok(Code.ADMIN_BLOCK_SELF_ERROR);
        }

        return adminService.block(adminId);
    }

    /**
     * 获取所有未删除角色
     *
     * @return 角色集合
     */
    @ApiOperation("获取所有未删除角色")
    @PostMapping("/selectAllNotDeleted")
    public Response<List<Role>> selectAllNotDeleted() {
        return roleService.selectAllNotDeleted();
    }
}
